package org.onionframework.persistence.dao;

import java.io.Serializable;
import java.util.List;

import org.onionframework.core.util.Reflections;
import org.onionframework.persistence.query.OnionQuery;

/**
 * 基础的HibernateDao
 * 
 * 使用sessionFactory的currentSession，无需自己管理session，也不用调用spring的template代码
 * 
 * @author allan
 *
 * @param <T> 实体类型
 * @param <PK> 主键类型
 */
public class GenericDaoImpl<T> extends CommonDaoImpl implements GenericDao<T>{
	
	protected Class<T> entityClass = Reflections.getClassGenricType(this.getClass());;

	public GenericDaoImpl() {
	}

	public GenericDaoImpl(java.lang.Class<T> persistClass) {
		this.entityClass = persistClass;
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#getEntityClass()
	 */
	@Override
	public Class<T> getEntityClass() {
		return this.entityClass;
	}
	
	void setEntityClass(Class<T> entityClass){
		this.entityClass = entityClass;
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#removeById(java.io.Serializable)
	 */
	@Override
	public void removeById(Serializable id) {
		super.removeById(this.entityClass, id);
	}
	
	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#get(java.io.Serializable)
	 */
	@Override
	public T get(Serializable id) {
		return super.get(entityClass, id);
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#load(java.io.Serializable)
	 */
	@Override
	public T load(Serializable id) {
		return super.load(entityClass, id);
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#getAll()
	 */
	@Override
	public List<T> getAll() {
		return super.findAllList(this.entityClass);
	}

	@Override
	public Long count(OnionQuery query) {
		return super.count(query.forClass(this.entityClass));
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#findUnique(java.lang.String, java.lang.Object)
	 */
	@Override
	public T findUnique(String name, Object value) {
		return super.findUnique(OnionQuery.instance(this.entityClass).eq(name, value));
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#findUnique(org.onionframework.persistence.query.OnionQuery)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public T findUnique(OnionQuery query) {
		return super.findUnique(query.forClass(this.entityClass));
	}
	
	protected List<T> find(String hql, Object... values) {
		return super.findList(hql, values);
	}

	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#find(org.onionframework.persistence.query.OnionQuery)
	 */
	@Override
	public List<T> find(OnionQuery query) {
		return super.findList(query.forClass(this.entityClass));
	}
	
	/* (non-Javadoc)
	 * @see org.onionframework.persistence.dao.GenericDao#find(java.lang.String, java.lang.Object)
	 */
	@Override
	public List<T> find(String name,Object value){
		return super.findList(OnionQuery.instance(this.entityClass).eq(name, value));
	}

	@SuppressWarnings("unchecked")
	protected List<T> findPage(String hql, int start, int maxResults,
			Object... parameters) {
		return super.findPage(hql, start, maxResults, parameters);
	}
}
